package com.zangbuge.hellonacos.config;

import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;

@Slf4j
@RestController
public class VersionController {

    /**
     * 打包后运行的jar才可以访问
     *
     * @return
     */
    @GetMapping("/version")
    public Object getVersion() {
        ClassLoader classLoader = getClass().getClassLoader();
        InputStream inputStream = classLoader.getResourceAsStream("git.properties");
        try (BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream, "utf-8"))) {
            StringBuffer buf = new StringBuffer();
            String line;
            while ((line = bufferedReader.readLine()) != null) {
                buf.append(line);
            }
            return buf.toString();
        } catch (Exception e) {
            log.error("获取版本信息异常", e);
        }
        return "error";
    }

}
